package cn.doctorpda.conference.presenter;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import cn.doctorpda.conference.MyApplication;
import cn.doctorpda.conference.bean.Msg;
import cn.doctorpda.conference.bean.UpdataInfo;
import cn.doctorpda.conference.bean.search.PageConInfo;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.config.NetConfig;
import cn.doctorpda.conference.event.IsNew;
import cn.doctorpda.conference.model.IGetModel;
import cn.doctorpda.conference.model.IPostModel;
import cn.doctorpda.conference.model.UpdataMomel;
import cn.doctorpda.conference.okhttp.callback.StringCallback;
import cn.doctorpda.conference.utils.AESUtil;
import cn.doctorpda.conference.utils.AppUtils;
import cn.doctorpda.conference.utils.GsonUtil;
import cn.doctorpda.conference.utils.L;
import cn.doctorpda.conference.utils.SPUtils;
import cn.doctorpda.conference.utils.ShareUtils;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.callbackview.IUpdateView;
import okhttp3.Call;

/**
 * Created by huangjinlong on 2016/5/25.
 */
public class UpdateVersionPresenter {
    private IPostModel model;
    private IUpdateView view;
    private UpdataInfo info;


    public UpdateVersionPresenter(IUpdateView view) {
        model = new UpdataMomel();
        this.view = view;
    }

    public void getUpdataInfo() {
        model.postNetWorkStringData(NetConfig.UPDATA, new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {

                view.setUpdateError(call, e);
            }

            @Override
            public void onResponse(String response) {

                try {
                    Msg msg = GsonUtil.fromJson(response, Msg.class);
                    JSONObject jsonObject = new JSONObject(response);
                    L.i("升级" + response);
                    if (jsonObject.getInt("code") == 0) {
                        String data = AESUtil.decrypt(msg.getData(), AppConfig.AES_KEY);
                        info = GsonUtil.fromJson(data, UpdataInfo.class);
                        L.i(data.toString());
                    } else if (jsonObject.getInt("code")  == 1) {
                        //已是最新版本
                        EventBus.getDefault().post(new IsNew("isNew"));

                    } else {
//                        T.showShort(,);
                        info = GsonUtil.fromJson(msg.getData(), UpdataInfo.class);
                        L.i("版本已最新" + info.toString());
                    }
//                   = GsonUtil.fromJson(object.getString("upgrade"), UpdataInfo.class);
                    view.setUpdateInfo(info);
                } catch (Exception e) {
                    UpdataInfo info = new UpdataInfo();
                    info.setVersion_code(AppUtils.getVersionCode(MyApplication.getInstance()));
                    view.setUpdateInfo(info);
                }

            }
        });
    }
}
